﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using System.Collections.Generic;
using System.Text;

public partial class Task_SplitTask : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTaskList();
            StartDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            EndDate.Text = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd");
            //StartDate.Attributes.Add("onchange", "CheckDate('" + StartDate.ClientID + "')");
            //EndDate.Attributes.Add("onchange", "EvalDate('" + StartDate.ClientID + "','" + EndDate.ClientID + "')");
            ButtonDelete.Visible = false;
            if (Request.QueryString["TaskId"] != null)
            {
                ButtonDelete.Visible = true;
                ButtonDelete.Attributes.Add("onclick", "return confirm('确定删除吗？')");
                BindData();
            }
        }
    }
    private void BindTaskList()
    {
        string[] CnNums = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三"  };
        StringBuilder sb;
        int j = 0;
        int k = 1;
        List<TaskInfo> tasks = Task.GetTasks(int.Parse(Request.QueryString["RootId"]));
        for (int i = 0; i < tasks.Count; i++)
        {
            TaskInfo task = tasks[i];
            sb = new StringBuilder();
            for (int m = 0; m < task.Depth; m++)
            {
                sb.Append("\u3000\u3000");
            }
            ListItem li = new ListItem();
            if (task.Depth == 1)
            {
                k = 1;
                li.Text = sb.ToString() + "第" + CnNums[j] + "阶段 " + task.Title;
                j++;
            }
            else if (task.Depth == 2)
            {
                li.Text = sb.ToString() + k.ToString() + "、" + task.Title;
                k++;
            }
            else
            {
                li.Text = sb.ToString() + task.Title;
            }
            li.Value = task.TaskId.ToString();
            TaskList.Items.Add(li);
        }
    }
    private void BindData()
    {
        TaskInfo task = Task.GetTask(int.Parse(Request.QueryString["TaskId"]));
        TaskList.SelectedIndex = TaskList.Items.IndexOf(TaskList.Items.FindByValue(task.TaskId.ToString()));
        TaskList.Enabled = false;
        TaskTitle.Text = task.Title;
        TaskComment.Text = task.Comment;
        StartDate.Text = task.StartDate.ToString("yyyy-MM-dd");
        EndDate.Text = task.EndDate.ToString("yyyy-MM-dd");
        txtLadePines.Text = task.LatePoints.ToString();
    }
    private void Insert()
    {
        TaskInfo task = new TaskInfo();
        task.TaskType = 0;
        task.ParentId = int.Parse(TaskList.SelectedValue);
        task.Title = TaskTitle.Text;
        task.Comment = TaskComment.Text;
        task.InitScore = 0;
        task.StartDate = DateTime.Parse(StartDate.Text);
        task.EndDate = DateTime.Parse(EndDate.Text);
        task.CreateDepartment = Yipusoft.Account.BLL.User.GetUser(Context.User.Identity.Name).DepartmentID;
        task.CreateUser = Yipusoft.Account.BLL.User.GetUser(Context.User.Identity.Name).UserName;
        task.CreateDate = DateTime.Now;
        task.UpdateDepartment = Yipusoft.Account.BLL.User.GetUser(Context.User.Identity.Name).DepartmentID;
        task.UpdateUser = Yipusoft.Account.BLL.User.GetUser(Context.User.Identity.Name).UserName;
        task.UpdateDate = DateTime.Now;
        task.Locked = false;
        task.LatePoints = Convert.ToDecimal(txtLadePines.Text);
        task.IsNextMonth = false;
        int taskId = Task.Insert(task);
        if (taskId > 0)
            Response.Redirect("SaveTask.aspx?RootId=" + Request.QueryString["RootId"] + "&TaskId=" + taskId.ToString());
    }
    private void Update()
    {
        TaskInfo task = Task.GetTask(int.Parse(Request.QueryString["TaskId"]));
        task.Title = TaskTitle.Text;
        task.Comment = TaskComment.Text;
        task.InitScore = 0;
        task.StartDate = DateTime.Parse(StartDate.Text);
        task.EndDate = DateTime.Parse(EndDate.Text);
        task.UpdateDepartment = Yipusoft.Account.BLL.User.GetUser(Context.User.Identity.Name).DepartmentID;
        task.UpdateUser = Yipusoft.Account.BLL.User.GetUser(Context.User.Identity.Name).UserName;
        task.UpdateDate = DateTime.Now;
        task.Locked = false;
        task.LatePoints = Convert.ToDecimal(txtLadePines.Text);
        task.IsNextMonth = false;
        Task.Update(task);
        Response.Redirect("SaveTask.aspx?RootId=" + Request.QueryString["RootId"] + "&TaskId=" + Request.QueryString["TaskId"]);
    }
    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["TaskId"] == null)
        {
            Insert();
        }
        else
        {
            Update();
        }
    }
    protected void ButtonDelete_Click(object sender, EventArgs e)
    {
        TaskInfo task = Task.GetTask(int.Parse(Request.QueryString["TaskId"]));
        if (task.Child == 0)
        {
            Task.Delete(int.Parse(Request.QueryString["TaskId"]));
            Response.Redirect("EditView.aspx?RootId=" + Request.QueryString["RootId"]);
        }
        else
        {
            Prompt.Text = "请先删除子项。";
        }
    }
    protected void ButtonCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("EditView.aspx?RootId=" + Request.QueryString["RootId"]);
    }
}
